<template>
<view class="container">
    <view class="topbar">
        <view :class="(status == '0' ? 'active' : '')" @tap="changeBar" data-status="0">全部</view>
        <view :class="(status == '-1' ? 'active' : '')" @tap="changeBar" data-status="-1">待付款</view>
        <view :class="(status == '1' ? 'active' : '')" @tap="changeBar" data-status="1">未消费</view>
        <view :class="(status == '2' ? 'active' : '')" @tap="changeBar" data-status="2">待评价</view>
    </view>
    <scroll-view scroll-y="true" :scroll-top="scrollTop" :style="'height:' + order_list_height + 'px'" bindscrolltolower="onListReachBottom">
        <block v-for="(item, order_list) in order_list" :key="order_list">
            <view class="item" :data-order_id="item.order_id" @tap="goDetail">
                <view class="header th100">订单编号：{{item.real_orderid}}</view>
                <view class="content cf">
                    <view class="fl">
                        <image :src="item.image"></image>
                    </view>
                    <view class="box">
                        <view class="name">{{item.name}}</view>
                        <view class="info">数量：{{item.num}} 总价：{{item.price}}元</view>
                    </view>
                </view>
                <view class="footer cf th80">
                    <view class="fl">
                        <text v-if="item.paid == 0 && item.status == 0">待支付</text>
                        <text v-else-if="item.status == 0">未接单</text>
                        <text v-else-if="item.status == 1">已确认</text>
                        <text v-else-if="item.status == 2">已消费</text>
                        <text v-else-if="item.status == 3">已评价</text>
                        <text v-else-if="item.status == 4">已退款</text>
                        <text v-else-if="item.status == 5">已取消</text>
                        <text v-else-if="item.status == 7">分配到自提点</text>
                        <text v-else-if="item.status == 8">发货到自提点</text>
                        <text v-else-if="item.status == 9">自提点接货</text>
                        <text v-else-if="item.status == 10">自提点发货</text>
                    </view>
                    <view class="fr" v-if="item.paid == 0 && (item.status < 2 || item.status == 7)" @click.stop="goPay" :data-index="index" :data-order_id="item.order_id">去付款</view>
                    <view class="fr" v-else-if="item.status == 2" @click.stop="goReply" :data-index="index" :data-order_id="item.order_id">去评价</view>
                </view>
            </view>
        </block>
    </scroll-view>
</view>
</template>

"<script>
import common from '../../utils/common.js'
var app = getApp();
var sysRes = {};
var rpxUnit = 0;
var page = 1;
var total = 0;
var go_order_id = 0;
var go_order_index = 0;

export default {
  data() {
    return {
      mer_id: 0,
      status: 0,
      order_list: [],
      order_list_height: 0,
      scrollTop: 0
    };
  },

  onUnload: function () {// 页面关闭
  },
  onHide: function () {// 页面隐藏
  },
  onShow: function () {
    // 页面显示
    console.log(go_order_id);

    if (go_order_id) {
      wx.showToast({
        title: "加载中",
        icon: 'loading',
        duration: 10000
      });
      common.post('Shop&a=order_detail', {
        order_id: go_order_id
      }, "setOneContent", this);
      go_order_id = 0;
      go_order_index = 0;
    }
	// #ifdef H5
	common.hideWxShare()
	// #endif
  },
  onReady: function () {
    // 页面渲染完成
    sysRes = wx.getSystemInfoSync();
    rpxUnit = sysRes.windowWidth / 750;
    this.setData({
      order_list_height: sysRes.windowHeight - 84 * rpxUnit
    });
  },
  onLoad: function (options) {
    if (!options.mer_id && app.merchantOwn) {
      options.mer_id = app.merchantOwn.mer_id;
    }

    if (options.mer_id) {
      this.setData({
        mer_id: options.mer_id
      });
    }

    var that = this;

    if (!app.globalData.userInfo) {
      //调用应用实例的方法获取全局数据
      common.getUserInfo(function (userInfo) {
        that.getOrderList();
      }, 'setting');
    } else {
      that.getOrderList();
    }
  },
  components: {},
  props: {},
  methods: {
    goPay: function (e) {
      go_order_id = e.currentTarget.dataset.order_id;
      go_order_index = e.currentTarget.dataset.index;
      wx.navigateTo({
        url: "../pay/index?order_id=" + e.currentTarget.dataset.order_id + '&type=shop'
      });
    },
    goReply: function (e) {
      go_order_id = e.currentTarget.dataset.order_id;
      go_order_index = e.currentTarget.dataset.index;
      wx.navigateTo({
        url: "my_reply?order_id=" + e.currentTarget.dataset.order_id
      });
    },
    goDetail: function (e) {
      console.log(e.currentTarget.dataset);
      go_order_id = e.currentTarget.dataset.order_id;
      go_order_index = e.currentTarget.dataset.index;
      wx.navigateTo({
        url: "shop_order_detail?order_id=" + e.currentTarget.dataset.order_id
      });
    },
    setOneContent: function (result) {
      console.log(result);
      var order_list = this.order_list;
      order_list[go_order_index] = result;
      this.setData({
        order_list: order_list
      });
      wx.hideToast();
    },
    setPageContent: function (result) {
      console.log(result.data);
      total = result.total;

      if (page == 1) {
        this.order_list = [];
      }

      this.setData({
        order_list: this.order_list.concat(result.list)
      });
      wx.hideToast();
    },
    getOrderList: function () {
      common.post('Shop&a=order_list', {
        page: page,
        status: this.status,
        mer_id: this.mer_id
      }, "setPageContent", this);
    },
    changeBar: function (e) {
      if (this.status == e.currentTarget.dataset.status) {
        return false;
      }

      this.setData({
        status: e.currentTarget.dataset.status,
        scrollTop: 0
      });
      page = 1;
      this.getOrderList();
    },
    onListReachBottom: function (e) {
      if (page >= total) {
        return false;
      }

      page++;
      this.getOrderList();
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
.topbar{
    display:flex;
    background-color: white;
}
.topbar view{
    width:25%;
    text-align: center;
    height:84rpx;
    line-height: 84rpx;
}
.topbar view.active{
    border-bottom:2rpx solid #06c1ae;
    color:#06c1ae;
}

.red{
    color: red;
}

.item{
    background:white;
    margin-top:24rpx;
}
.item:last-child{
    margin-bottom:16rpx;
}
.header{
    color:#9E9E9E;
    padding-left:24rpx;
}
.content{
    background-color:#F2F2F2;
    padding:16rpx 24rpx;
}
.content image{
    height:140rpx;
    width:188rpx;
    vertical-align: middle;
}
.box{
    margin-left:200rpx;
}
.name{
    margin-top:20rpx;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.info{
    color:#9E9E9E;
    margin-top:12rpx;
    font-size:24rpx;
}
.footer{
    color:#06c1ae;
    padding:15rpx 24rpx;
    height:70rpx;
    line-height: 70rpx;
}
.footer .fr{
    padding:8rpx 40rpx;
    border:2rpx solid #e5e5e5;
    height:50rpx;
    line-height:50rpx;
    border-radius:8rpx;
}
</style>